package com.csun.dsms.controller;

import com.csun.dsms.comment.core.annotation.Authorization;
import com.csun.dsms.comment.core.annotation.CurrentUser;
import com.csun.dsms.comment.core.utils.wrapper.WrapMapper;
import com.csun.dsms.comment.core.utils.wrapper.Wrapper;
import com.csun.dsms.model.domain.DsmsArea;
import com.csun.dsms.model.domain.DsmsCompany;
import com.csun.dsms.model.domain.DsmsUnit;
import com.csun.dsms.model.domain.DsmsUser;
import com.csun.dsms.model.dto.staff.StaffQueryDto;
import com.csun.dsms.model.vo.AreaUnitVo;
import com.csun.dsms.model.vo.CompanyVo;
import com.csun.dsms.model.vo.company.CompanyInfoVo;
import com.csun.dsms.model.vo.menu.MenuVo;
import com.csun.dsms.model.vo.staff.StaffVo;
import com.csun.dsms.service.DsmsAreaService;
import com.csun.dsms.service.DsmsCompanyService;
import com.csun.dsms.service.DsmsStaffService;
import com.csun.dsms.service.DsmsUnitService;
import com.csun.dsms.utils.PublicUtil;
import com.csun.dsms.utils.TreeUtil;
import com.csun.dsms.validator.DsmsCompanyValidator;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@RestController
@RequestMapping("/company")
public class DsmsCompanyController {

    @Resource
    private DsmsCompanyService dsmsCompanyServiceImpl;
    @Resource
    private DsmsUnitService dsmsUnitServiceImpl;
    @Resource
    private DsmsStaffService dsmsStaffServiceImpl;

    /*
    * 服务公司的添加
    * */
    @Authorization
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Wrapper companyAdd(@CurrentUser DsmsUser login,@RequestBody DsmsCompany dsmsCompany){
        dsmsCompany.setCreator(login.getLoginName());
        dsmsCompany.setCreatorId(login.getId());
        dsmsCompany.setLastOperator(login.getLoginName());
        dsmsCompany.setLastOperatorId(login.getId());
        /*
        * 判断公司名是否重复
        * */
        DsmsCompany company = new DsmsCompany();
        company.setName(dsmsCompany.getName());
        if(PublicUtil.isEmpty(company.getName().trim())){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"公司名称为空");
        }
        List<DsmsCompany> companies = dsmsCompanyServiceImpl.queryByCondition(company);
        if(companies.size() != 0){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"公司名称已存在");
        }
        company.setName("");
        company.setSocialCode(dsmsCompany.getSocialCode());
        if(PublicUtil.isEmpty(company.getSocialCode())){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"统一社会信用代码为空");
        }
        DsmsUnit unit = new DsmsUnit();
        unit.setSocialCode(dsmsCompany.getSocialCode());
        List<DsmsUnit> query = dsmsUnitServiceImpl.query(unit);
        if(query.size() != 0){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"统一社会信用代码已存在");
        }

        DsmsUnit dsmsUnit = new DsmsUnit();
        BeanUtils.copyProperties(dsmsCompany,dsmsUnit);
        dsmsUnit.setUnitType(2);
        Integer save = dsmsUnitServiceImpl.save(dsmsUnit);
        dsmsCompany.setId(dsmsUnit.getId());
        Integer i = dsmsCompanyServiceImpl.save(dsmsCompany);
        if(i<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE);
    }

    /*
     * 服务公司的删除
     * */
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public Wrapper companyDelete(@RequestBody DsmsCompany dsmsCompany){
        /*
        * 查询该公司下是否有在职服务人员
        * */
        StaffQueryDto staffQueryDto = new StaffQueryDto();
        staffQueryDto.setUnitId(dsmsCompany.getId());
        staffQueryDto.setStatus("ENABLE");
        List<StaffVo> staffVos = dsmsStaffServiceImpl.queryStaffVo(staffQueryDto);
        if(staffVos.size()>0){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"该公司有在职员工");
        }
        Integer delete = dsmsUnitServiceImpl.deleteByKey(dsmsCompany);
        Integer i = dsmsCompanyServiceImpl.deleteByKey(dsmsCompany);
        if(i<1 || delete<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE);
    }

    /*
     * 服务公司的修改
     * */
    @Authorization
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Wrapper companyUpdate(@CurrentUser DsmsUser login,@RequestBody DsmsCompany dsmsCompany){
        /*
         * 判断公司名是否重复
         * */
        DsmsCompany company = new DsmsCompany();
        company.setName(dsmsCompany.getName());
        if(PublicUtil.isEmpty(company.getName().trim())){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"公司名称为空");
        }
        List<DsmsCompany> companies = dsmsCompanyServiceImpl.queryByCondition(company);
        if(companies.size() != 0 && companies.get(0).getId() != dsmsCompany.getId()){
            for (DsmsCompany c:companies) {
                if(!c.getId().equals(dsmsCompany.getId())){
                    return WrapMapper.wrap(Wrapper.ERROR_CODE,"公司名称已存在");
                }
            }
        }
        company.setName("");
        company.setSocialCode(dsmsCompany.getSocialCode());
        if(PublicUtil.isEmpty(company.getSocialCode())){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"统一社会信用代码为空");
        }
        companies = dsmsCompanyServiceImpl.query(company);
        if(companies.size() != 0){
            for (DsmsCompany c:companies) {
                if(!c.getId().equals(dsmsCompany.getId())){
                    return WrapMapper.wrap(Wrapper.ERROR_CODE,"统一社会信用代码已存在");
                }
            }
        }
        dsmsCompany.setLastOperator(login.getLoginName());
        dsmsCompany.setLastOperatorId(login.getId());
        dsmsCompany.setUpdateTime(new Date());
        DsmsUnit dsmsUnit = new DsmsUnit();
        BeanUtils.copyProperties(dsmsCompany,dsmsUnit);
        Integer update = dsmsUnitServiceImpl.update(dsmsUnit);
        Integer i = dsmsCompanyServiceImpl.update(dsmsCompany);
        if(i<1 && update<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE);
    }

    /*
     * 服务公司的查询
     * */
    @RequestMapping(value = "/query",method = RequestMethod.POST)
    public Wrapper companyQuery(@RequestBody DsmsCompany dsmsCompany){
        List<DsmsCompany> dsmsCompanies = dsmsCompanyServiceImpl.query(dsmsCompany);
        if(dsmsCompanies.size() == 0){
            DsmsCompany company = new DsmsCompany();
            company.setId(dsmsCompany.getPid());
            company.setName(dsmsCompany.getName());
            List<DsmsCompany> query = dsmsCompanyServiceImpl.query(company);
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,new PageInfo<>(query));
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,new PageInfo<>(dsmsCompanies));
    }

    /*
     * 服务公司菜单栏
     * */
    @RequestMapping(value = "/tree",method = RequestMethod.POST)
    public Wrapper companyTree(){
        List<DsmsCompany> dsmsCompanies = dsmsCompanyServiceImpl.queryCompany(null);
        List<MenuVo> menuVos = new ArrayList<>();
        for (DsmsCompany company:dsmsCompanies) {
                MenuVo menuVo = new MenuVo();
                BeanUtils.copyProperties(company,menuVo);
                menuVo.setMenuName(company.getName());
                menuVos.add(menuVo);
        }
        List<MenuVo> childMenuVos = TreeUtil.getChildMenuVos(menuVos, 0L);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,childMenuVos);
    }



    /**
     * 根据单位查询服务公司
     * @param unitId
     * @return
     */
    @PostMapping("/queryCompanyByUnitId")
    public Wrapper queryCompanyByUnitId(Long unitId){
        CompanyInfoVo companyInfoVo = dsmsCompanyServiceImpl.selectByUnitId(unitId);
        if(companyInfoVo == null || companyInfoVo.getId() == null){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE,"未绑定服务公司");
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,companyInfoVo);
    }

}
